package oop.controller.action.user;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oop.controller.action.DefaultAction;
import oop.db.Database;

@SuppressWarnings("unused")
public class SignUpAction extends UserManagerAction {

	@Override
	public void perform()
			throws Exception {
		if ("Sign Up".equals(request.getParameter("signUp"))) {
			int error = 0;
			if (isUserExist(request, response)) {
				// nguoi dung da ton tai
				request.setAttribute("userExist", "true");
				error++;
			}
			if (isEmailExist(request, response)) {
				// email da duoc dung
				request.setAttribute("emailUnavailable", "true");
				error++;
			}
			if (isValidUserName(request, response) == false) {
				// Ten nguoi dung nhap sai qui cach
				request.setAttribute("invalidUserName", "true");
				error++;
			}
			if (isValidEmail(request, response) == false) {
				// Email sai qui cach
				request.setAttribute("invalidEmail", "true");
				error++;
			}
			if (isValidPassword(request, response) == false) {
				// Password sai qui cach
				request.setAttribute("invalidPass", "true");
				error++;
			}

			if (error > 0) {
				request.setAttribute("error", "true");
			} else {

				Connection conn;
				try {
					conn = Database.get().getConnection();
					Statement stmt = conn.createStatement();
					String sql = "INSERT INTO tracnghiem.tblUser ("
							+ "user_name ," + "user_fullname ," + "user_pass ,"
							+ "user_email ," + "user_group ,"
							+ "user_register_date)" + " VALUES (" + "'"
							+ request.getParameter("userName") + "', " + "'"
							+ request.getParameter("fullName") + "', " + "'"
							+ request.getParameter("password") + "', " + "'"
							+ request.getParameter("userEmail") + "', "
							+ "'user', " + "curdate())";

					int result = stmt.executeUpdate(sql);
					if (result > 0){
						request.getSession(true).setAttribute("username",
								request.getParameter("userName"));
						request.getSession(true)
								.setAttribute("login", true);
						request.getSession(true)
							.setAttribute("notlogin", false);
						setNextAction("homepage");
					}else {
						request.setAttribute("sqlError", "true");
					}
				} catch (SQLException e) {
					request.setAttribute("sqlError", "true");
					request
							.getRequestDispatcher(
									"template/user-signup-GUI.jsp").forward(
									request, response);
					e.printStackTrace();
				}
			}
		}
	}

	private boolean isValidUserName(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String userName = request.getParameter("userName");
		String userExp = "[a-zA-Z]+[a-zA-Z0-9]";
		if ((userName.matches(userExp))&&(isEmpty(userName) == false))
			return true;
		else return false;
	}

	private boolean isValidPassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String pass = request.getParameter("password");
		String confPass = request.getParameter("confirmPass");
		if ((pass.compareTo(confPass) != 0) || (isEmpty(pass)))
			return false;
		return true;
	}

}
